---
layout: post
date: 2024-01-30
title: "Why is Simple Code So Often Bad?"
description: "Can complex software succeed when we struggle to write simple code?"
tags: [design]
---

<p>I recently wrote a <a href="https://github.com/karlseguin/metrics.zig">Prometheus client library for Zig</a>. I had to write a custom hash map context (what Zig calls the <code>eql</code> and <code>hash</code> functions) which led to my last couple blog posts exploring Zig's hash maps in more details. The last topic covered was the <code>getOrPut</code> method which returns a pointer to the key and value arrays of where the entry is or should be. <code>getOrPut</code> is great because you can do an upsert operation with a single lookup (but, like all things in GC-free language, don't hold on to that reference else you risk a dangling pointer).</p>

<p>While writing the library, I used the Go client as a reference. I also looked through a Node.js implementation. There I saw:</p>

{% highlight javascript %}
updateExemplar(exemplarLabels, value, hash) {
  if (!isObject(this.hashMap[hash].exemplar)) {
    this.hashMap[hash].exemplar = new Exemplar();
  }
  this.hashMap[hash].exemplar.validateExemplarLabelSet(exemplarLabels);
  this.hashMap[hash].exemplar.labelSet = exemplarLabels;
  this.hashMap[hash].exemplar.value = value ? value : 1;
  this.hashMap[hash].exemplar.timestamp = nowTimestamp();
}
{% endhighlight %}

<p>To me, this code is problematic for two reasons. While I know nothing about V8 optimization, I assume 5 or 6 hash lookups is going to be slower than 1 or 2. The other issue is that, as it's written, you need to parse each line to understand that a single object is being mutated. A local would solve both issues:</p>

{% highlight javascript %}
updateExemplar(exemplarLabels, value, hash) {
  let exemplar = this.hashMap[hash].exemplar;

  if (!isObject(exemplar)) {
    exemplar = new Exemplar();
    this.hashMap[hash].exemplar = exemplar;
  }

  exemplar.validateExemplarLabelSet(exemplarLabels);
  exemplar.labelSet = exemplarLabels;
  exemplar.value = value ? value : 1;
  exemplar.timestamp = nowTimestamp();
}
{% endhighlight %}

<p>The original code isn't <em>awful</em>. But it <strong>is</strong> a simple function and it seems to almost go out of its way to be inefficient to run, read and maintain. It honestly makes me wonder how it happens. I don't mean to pick on this one example, even though I know I am. It just happens to be the most recent example I've seen. The real issue is how commonplace this is. I wonder how often I've done something like it, or worse, without realizing. I just finished writing a multipart/form-data parser, and it's <em>awful</em>. </p>

<p>I'm skeptical about our ability to improve, and wonder how close we are to hitting a limit on the complexity of software that can be written and maintained.</p>
